
var curUser = null;


function ajaxPostJson(url,token, data, callback) { 
    var xhr = new XMLHttpRequest();
    xhr.open('POST', uri_prefix + url, true);

    // 设置多个 HTTP 头 
    xhr.setRequestHeader("Content-Type","application/json");
    if(token!=null){
		xhr.setRequestHeader("token",token);
	}
 
    // 处理响应
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status >= 200 && xhr.status < 300) {
				var res = null;
				var error = null;
                try {
                    res = JSON.parse(xhr.responseText); 
                } catch (error) {
					error = "JSON 解析错误:" + error.message;
                    //callback(-1,"JSON 解析错误:" + error.message);
                }
				if(error == null){
					callback(0, res);
				}else{
					callback(-1, error);
				}
            } else {
                callback(1,"请求错误，状态码:" + xhr.status);
            }
        }
    };
	xhr.onerror = function () {
		callback(-1, "Post访问("+url+")发生错误");
	}; 
	xhr.send(JSON.stringify(data)); // 发送 JSON 数据 
}

function ajaxPostText(url,token, data, callback) { 
    var xhr = new XMLHttpRequest();
    xhr.open('POST', uri_prefix + url, true);

    // 设置多个 HTTP 头 
    xhr.setRequestHeader("Content-Type","html/text");
    if(token!=null){
		xhr.setRequestHeader("token",token);
	}
    // 处理响应
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status >= 200 && xhr.status < 300) {
				var res = null;
				var error = null;
                try {
                    res = xhr.responseText; 
                } catch (error) {
					error = "获取返回值错误:" + error.message; 
                }
                if(error == null){
					callback(0, res);
				}else{
					callback(-1, error);
				}
            } else {
                callback(1,"请求错误，状态码:" + xhr.status);
            }
        }
    };
	xhr.onerror = function () {
		callback(-1, "Post访问("+url+")发生错误");
	}; 
	xhr.send(data); // 发送字符串
}

function ajaxGetJson(url, callback , prefix=false,token=null) { 
    var xhr = new XMLHttpRequest();
    //xhr.open('GET', uri_prefix + url, true);
	if(prefix==true){
		xhr.open('GET', uri_prefix + url, true);
	}else{
		xhr.open('GET', url, true);
	} 		
    // 设置多个 HTTP 头 
    xhr.setRequestHeader("Content-Type","application/json");
    if(token!=null){
		xhr.setRequestHeader("token",token);
	}
 
    // 处理响应
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status >= 200 && xhr.status < 300) {
				var res = null;
				var error = null;
                try {
                    res = JSON.parse(xhr.responseText); 
                } catch (error) {
                    error = "JSON 解析错误:" + error.message; 
                }
                if(error == null){
					callback(0, res);
				}else{
					callback(-1, error);
				}
            } else {
                callback(1,"请求错误，状态码:" + xhr.status);
            }
        }
    };
	xhr.onerror = function () {
		callback(-1, "Get访问("+url+")发生错误");
	}; 
	xhr.send(); 
}

function ajaxGetText(url, callback, prefix=false,token=null) { 
    var xhr = new XMLHttpRequest();
	if(prefix==true){
		xhr.open('GET', uri_prefix + url, true);
	}else{
		xhr.open('GET', url, true);
	} 
    // 设置多个 HTTP 头 
    xhr.setRequestHeader("Content-Type","html/text");
    if(token!=null){
		xhr.setRequestHeader("token",token);
	}
 
    // 处理响应
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status >= 200 && xhr.status < 300) {
				var res = null;
				var error = null;
                try {
                    res = xhr.responseText; 
                } catch (error) {
                    error = "获取返回值错误:" + error.message; 
                }
                if(error == null){
					callback(0, res);
				}else{
					callback(-1, error);
				}
            } else {
                callback(1,"请求错误,状态码:" + xhr.status);
            }
        }
    };
	xhr.onerror = function () {
		callback(-1, "Get访问("+url+")发生错误");
	}; 
	xhr.send(); // 发送字符串
}

function uploadFile(url, token, file, callBack) { 

	const formData = new FormData();
	formData.append("file", file);

	// 在函数内部定义 xhr 变量
	const xhr = new XMLHttpRequest();
	xhr.open("POST", url, true); 
			
	xhr.setRequestHeader("token",curUser["token"]);  
		    
	xhr.onload = function () { 
	   if (xhr.status >= 200 && xhr.status < 300) {
		    var res = null;
		    var error = null;
		    try {
	             res = JSON.parse(xhr.responseText); 
	        } catch (error) {
		        error = "JSON 解析错误:" + error.message; 
	        } 
	        if(error == null){
	            callBack(0, res);
		    }else{
			    callBack(-1, error);
		    }
	    } else {
	        callBack(1,"上传错误，状态码:" + xhr.status);
	    } 
	};
	
	xhr.onerror = function () {
	 	callback(-1, "上传错误！");
	}; 
	xhr.send(formData); 
}
  

var us = localStorage.getItem("cuser");
if(us != null){
	curUser = JSON.parse(us);
	console.log("curUser:"+us);
}
	
